package com.clive.ttwebdemo.controller;

import com.clive.ttwebdemo.bean.Dishes;
import com.clive.ttwebdemo.service.DishServiceCategory;
import com.clive.ttwebdemo.service.DishesService;
import com.clive.ttwebdemo.vo.DishResult;
import com.clive.ttwebdemo.vo.DishesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.FileOutputStream;

//他是springmv的注解 是一个controller类接收请求生成响应
@RestController
@RequestMapping("/dish")
@CrossOrigin
public class DishController {
    @Autowired
    private DishesService dishesService;
    @RequestMapping("/addDishes")
    public DishResult addDishes(@RequestBody @Valid Dishes dishes) {
        boolean b = dishesService.addDishes(dishes);
        if(b){
            return DishResult.ok();
        }
        return DishResult.error("添加菜品失败");
    };
    @RequestMapping("/findOne/(id)")
    public DishResult findOne(@PathVariable Integer id){
        Dishes dishes = dishesService.findDishById(id);
        return  DishResult.ok().date("dishes",dishes);
    };
    @RequestMapping("/update")
    public DishResult upadte(@Valid @RequestBody Dishes dishes){
        boolean b = dishesService.updateDish(dishes);
        if(b){
            return DishResult.ok();
        }
        return DishResult.error("修改菜品失败");
    };
    @RequestMapping("/delete/(id)")
    public DishResult deleteDish(@RequestBody Integer id){
         boolean b= dishesService.deleteDish(id);
        if(b){
            return DishResult.ok();
        }
        return DishResult.error("删除菜品失败");
    }
    @RequestMapping("/query")
    public DishResult queryDish(@RequestBody Dishes dishes){
        DishesVo vo = dishesService.queryDish(dishes);
        return DishResult.ok().date("vo",vo);
    }
}
